@file:JvmName("LegacyPrinter")

package com.jakewharton.sdksearch.api.dac.legacy

import com.jakewharton.sdksearch.api.dac.DacComponent
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
import okhttp3.OkHttpClient

suspend fun main() {
  val client = OkHttpClient()
  val service = DacComponent.create(client)
      .documentationService()

  val items = service.list().sortedWith(compareBy({ it.packageName }, { it.className }))

  // Shut down OkHttpClient resources so that the JVM can exit cleanly.
  client.dispatcher.executorService.shutdown()
  client.connectionPool.evictAll()

  println("// Generated by https://github.com/JakeWharton/SdkSearch/tree/master/api/dac-proxy/legacy-printer")
  println("// ${DateTimeFormatter.RFC_1123_DATE_TIME.format(ZonedDateTime.now())}")
  println("var DATA = [")
  items.forEachIndexed { index, item ->
    val deprecated = item.deprecated
    val type = "class"
    val link = item.link.removePrefix("https://developer.android.com/")
    val fqcn = "${item.packageName}.${item.className}"
    println("""      { id:$index, label:"$fqcn", link:"$link", type:"$type", deprecated:"$deprecated" },""")
  }
  println("];")
}
